module.exports = class extends think.cmswing.rest {
  /**
   * dingdan
   * @returns {Promise.<*>}
   */
  async postAction() {
    // 买方信息
    const gongxufabuid = this.post('id');// 供需发布id
    const yewuleixing = this.post('yewuleixing');
    const gongid = this.post('gongid');
    const gong_mobile = this.post('gong_mobile');
    const gong_username = this.post('gong_username');
    const gong_gsmc = this.post('gong_gsmc');
    const xu_gsmc = this.post('xu_gsmc');
    const xu_username = this.post('xu_username');
    const xu_mobile = this.post('xu_mobile');
    const xuid = this.post('xuid');
    const order_no = await this.model('cmswing/order').orderid(gongid);
    const xinxibiaoti = this.post('xinxibiaoti');
    const shangpingleibie = this.post('shangpingleibie');
    const leirongmiaoshu = this.post('leirongmiaoshu');
    const diqu = this.post('diqu');
    const chuchenglv = this.post('chuchenglv');
    const suozaiweizhi = this.post('suozaiweizhi');
    const jiage = this.post('jiage');
    const shangpingshuliang = this.post('shangpingshuliang');
    const zongjia = this.post('zongjia');
    const fabushijian = this.post('fabushijian');
    const chuangjianshijian = new Date().toLocaleString();
    const status = 0;
    const taxes = this.post('taxes');// 税率
    const persent = this.post('persent');// 付款比例
    const shifouhetong = this.post('shifouhetong');// 是否线下合同
    const mokuaibiaoshi = this.post('mokuaibiaoshi');// 用于区别不同模块的订单
    const tupianid = this.post('tupianid');
    let tupian = this.post('tupian');
    let tupianall;
    if (!think.isEmpty(tupianid)) {
      tupian = await this.model('ext_attachment_pic').where({ id: ['IN', tupianid] }).getField('path');
      tupianall = JSON.stringify(tupian);
    } else {
      tupianall = tupian;
    }
    const data = {
      order_no: order_no,
      gongxufabuid: gongxufabuid,
      yewuleixing: yewuleixing,
      gongid: gongid,
      gong_mobile: gong_mobile,
      gong_username: gong_username,
      gong_gsmc: gong_gsmc,
      xu_gsmc: xu_gsmc,
      xu_username: xu_username,
      xu_mobile: xu_mobile,
      xuid: xuid,
      chuchenglv: chuchenglv,
      xinxibiaoti: xinxibiaoti,
      shangpingleibie: shangpingleibie,
      leirongmiaoshu: leirongmiaoshu,
      diqu: diqu,
      suozaiweizhi: suozaiweizhi,
      shangpingshuliang: shangpingshuliang,
      jiage: jiage,
      zongjia: zongjia,
      taxes: taxes,
      shifouhetong: shifouhetong,
      tupian: tupianall,
      fabushijian: fabushijian,
      chuangjianshijian: chuangjianshijian,
      status: status,
      mokuaibiaoshi: mokuaibiaoshi,
      persent: persent
    };
    let querenren;
    if (gongxufabuid) {
      const gongxuinfo = await this.model('gongxufabu').where({ id: gongxufabuid }).find();
      data.info_channel = gongxuinfo.info_channel;
      data.mokuaibiaoshi = gongxuinfo.mokuaibiaoshi;
      data.order_userid = data.gongid;
      querenren = 'gongqueren';
      if (data.order_userid == gongxuinfo.faburen) {
        data.order_userid = data.xuid;
        querenren = 'xuqueren';
      }
    } else {
      if (data.yewuleixing == 0) {
        data.order_userid = data.gongid;
        querenren = 'gongqueren';
      } else if (data.yewuleixing == 1) {
        data.order_userid = data.xuid;
        querenren = 'xuqueren';
      }
      if (data.xinxibiaoti == '点就收') {
        data.info_channel = 1;
        data.mokuaibiaoshi = 0;
      } else if (data.xinxibiaoti == '商品下单') {
        data.info_channel = 2;
        data.mokuaibiaoshi = 2;
      }
    }
    data[querenren] = 1;
    const order_id = await this.model('order').add(data);
    if (gongxufabuid) {
      const messageType = this.post('messageType');
      const update = { status: 6 };
      if (messageType == 'accept') {
        update.status = 4;
      }
      const gongxu = await this.model('gongxufabu').where({ id: gongxufabuid }).update(update);// 如果挂单存在，就下架状态
    }
    if (!order_id) { return this.fail('订单新增失败') };
    return this.success('订单新增成功');
  }

  async getAction() {
    const id = this.get('id');
    const user_id = this.get('user_id');
    const type = this.get('type') || null;
    let orderAll = [];
    if (id && !user_id) {
      orderAll = await this.model('cmswing/order').where({ id: id }).find();
      if (!orderAll) { return this.fail('暂无订单') };
      return this.success(orderAll, '订单查询成功');
    } else if (!id && user_id) {
      const typeMap = {
        null: {
          _complex: { xuid: user_id, gongid: user_id, _logic: 'OR' }
        },
        0: {// 未确认
          status: 0,
          order_userid: ['!=', user_id],
          _complex: [{
            xuid: user_id,
            gongid: user_id,
            _logic: 'OR'
          },
          {
            _complex: [
              {
                gongqueren: 0,
                xuqueren: 1
              },
              {
                gongqueren: 1,
                xuqueren: 0
              }
            ],
            _logic: 'OR'
          }
          ]
        },
        1: {// 采购订单
          xuid: user_id,
          yewuleixing: ['in', [0, 1]]
        },
        2: {// 销售订单
          gongid: user_id,
          yewuleixing: ['in', [0, 1]]
        },
        3: {// 委托加工
          xuid: user_id,
          yewuleixing: ['in', [2, 3]]
        },
        4: {// 承接加工
          gongid: user_id,
          yewuleixing: ['in', [2, 3]]
        }
      };
      const map = typeMap[type];
      orderAll = await this.model('cmswing/order').where(map).order('id DESC').page(this.para('page') || 1, 10).select();
      if (!orderAll) { return this.fail('暂无订单') };
      return this.success(orderAll, '订单查询成功');
    } else {
      return this.fail('参数传递错误');
    }
  }
  async tobeconfirmAction() {
    const userId = this.get('user_id');
    if (userId) {
      const orderAll = await this.model('order').where({// 未确认
        status: 0,
        order_userid: ['!=', userId],
        _complex: [{
          xuid: userId,
          gongid: userId,
          _logic: 'OR'
        },
        {
          _complex: [
            {
              gongqueren: 0,
              xuqueren: 1
            },
            {
              gongqueren: 1,
              xuqueren: 0
            }
          ],
          _logic: 'OR'
        }
        ]
      }).order('id DESC').select();
      if (!orderAll) { return this.fail('暂无订单') };
      return this.success(orderAll, '订单查询成功');
    } else {
      return this.fail('参数传递错误');
    }
  }
};
